import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () =>
    import ( /* webpackChunkName:"login_home_welcome" */ '@/components/Login.vue');
const Home = () =>
    import ( /* webpackChunkName:"login_home_welcome" */ '@/components/Home.vue');
const Welcome = () =>
    import ( /* webpackChunkName:"login_home_welcome" */ '@/components/Welcome.vue');

const Users = () =>
    import ( /* webpackChunkName:"Users" */ '@/components/user/Users.vue');
const Roles = () =>
    import ( /* webpackChunkName:"Roles" */ '@/components/role/Roles.vue');
const Rights = () =>
    import ( /* webpackChunkName:"Rights" */ '@/components/role/Rights.vue');
const Reports = () =>
    import ( /* webpackChunkName:"Reports" */ '@/components/report/Reports.vue');

const Orders = () =>
    import ( /* webpackChunkName:"Orders" */ '@/components/order/Orders.vue');
const Categories = () =>
    import ( /* webpackChunkName:"Categories" */ '@/components/goods/Categories.vue');
const Goods = () =>
    import ( /* webpackChunkName:"Goods" */ '@/components/goods/Goods.vue');
const Params = () =>
    import ( /* webpackChunkName:"Params" */ '@/components/goods/Params.vue');
const AddGood = () =>
    import ( /* webpackChunkName:"AddGood" */ '@/components/goods/AddGood.vue');
// import Home from '@/components/Home.vue'.@代表src 目录，不可以用.表示当前路
// import Welcome from '@/components/Welcome.vue'

// import Users from '@/components/user/Users.vue'
// import Roles from '@/components/role/Roles.vue'
// import Rights from '@/components/role/Rights.vue'
// import Reports from '@/components/report/Reports.vue'
// import Orders from '@/components/order/Orders.vue'
// import Categories from '@/components/goods/Categories.vue'
// import Goods from '@/components/goods/Goods.vue'
// import Params from '@/components/goods/Params.vue'
// import AddGood from '@/components/goods/AddGood'
Vue.use(VueRouter)
const routes = [{
        path: '/',
        redirect: '/login'
    }, {
        path: '/login',
        component: Login
    },
    {
        path: '/home',
        redirect: '/Welcome',
        component: Home,
        children: [{
            path: '/Welcome',
            component: Welcome
        }, {
            path: '/users',
            component: Users
        }, {
            path: '/roles',
            component: Roles
        }, {
            path: '/rights',
            component: Rights
        }, {
            path: '/reports',
            component: Reports
        }, {
            path: '/orders',
            component: Orders
        }, {
            path: '/categories',
            component: Categories
        }, {
            path: '/goods',
            component: Goods
        }, {
            path: '/params',
            component: Params
        }, {
            path: '/addGood',
            component: AddGood
        }, {
            path: '/addGood?id=:id',
            component: AddGood
        }]
    }
]

const router = new VueRouter({
        routes
    })
    //在路由器上面挂载路由守卫
router.beforeEach((to, from, next) => {
    //如果访问的是登录页面，就不守卫
    if (to.path == '/login') return next()
    const token = window.sessionStorage.getItem('token')
        //注意next函数可以接受一个path
    if (!token) return next('/login')
        //如果不符合前两项条件，则放行
    next()
})
export default router